﻿using System;
using System.Web.UI;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;

namespace SLMySitesNavInjection.ControlTemplates.SLMySitesNavInjection
{
    public partial class SLMySitesNavInjectionUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                RegisterJsFiles();
                RegisterNavigationElementsJs();
            }
            catch (Exception ex)
            {
                //log error
                UlsHelper.WriteUlsEntry(ex, TraceSeverity.Unexpected, EventSeverity.Error, Definitions.UlsCategory);
            }
        }

        /// <summary>
        /// Creates and registers the JavaScript menu items as a JavaScriptArray
        /// </summary>
        internal void RegisterNavigationElementsJs()
        {
            var jsArray = "";
            //process all the defined links
            for (var i = 0; i < Definitions.ProfilePageLinks.GetLength(0); i++)
            {
                var pageUrl = Definitions.ProfilePageLinks[i, Definitions.LinkUrl];
                var pageTitle = Definitions.ProfilePageLinks[i, Definitions.LinkTitle];
                var selectedClass = "";
                var selectedSpan = "";
                //check to see if the page item being generated is the current page, if so add the required HTML
                if (IsCurrentPage(pageUrl))
                {
                    selectedClass = "selected ms-core-listMenu-selected";
                    selectedSpan = Definitions.SelectedSpan;
                }
                //add the querystring to the links (handles looking at other profiles e.g. accountname=DOMAN\login)
                if (!string.IsNullOrEmpty(Request.QueryString.ToString()))
                {
                    pageUrl += "?" + Request.QueryString;
                }
                jsArray += string.Format(Definitions.NavItemTemplate, selectedClass, pageUrl, pageTitle, selectedSpan);
            }

            var jsToInsert = string.Format(Definitions.JsTemplate, jsArray);
            Page.ClientScript.RegisterClientScriptBlock(GetType(), "SLMySitesNewNavItems", jsToInsert, true);
        }

        /// <summary>
        /// Checks to see if the current item is the currently selected page
        /// </summary>
        /// <param name="pageUrl"></param>
        /// <returns>true if the item is the currently selected page, false otherwise</returns>
        internal bool IsCurrentPage(string pageUrl)
        {
            return Request.Url.LocalPath.ToLower().Contains(pageUrl);
        }

        /// <summary>
        /// Registers all the include files required for the web part
        /// </summary>
        internal void RegisterJsFiles()
        {
            var assemblyVersion = "?ver=" + Definitions.GetAssemblyFileVersion();
            ScriptLink.RegisterScriptAfterUI(Page, Definitions.JqueryIncludePath, false);
            ScriptLink.RegisterScriptAfterUI(Page, Definitions.ControlJsIncludePath + assemblyVersion, false, false);
        }
    }
}
